<template>
  <view v-if="shenheStatus == 0" class="page">
    <view class="page-content">
      <template v-if="listData.length > 0">
        <view class="list-item" v-for="(item, index) in listData" :key="index">
          <view class="record-item">
            <view class="record-info">
              <view>{{ item.desc }}</view>
              <view>{{ item.created_at | timestampToTime }}</view>
            </view>
            <view
              class="record-money"
              :class="{ 'font-black': item.log_type == 2 }"
              >{{ item.log_type == 2 ? '-' : '+'
              }}{{ item.amount | formatNum }}</view
            >
          </view>
        </view>
      </template>
    </view>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from '@/components/shenhe/shenhe';
export default {
  components: { Shenhe },
  data() {
    return {
      listData: [],
    };
  },
  onLoad(t) {
    uni.setNavigationBarTitle({
      title: `${this.stores.integral_name}明细`,
    });
    this.getRecordList();
  },
  filters: {
    formatNum(num) {
      let str = String(num).replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
      if (str.split('.').length == 1) {
        str = str + '.00';
      }
      return str;
    },

    timestampToTime(timestamp) {
      let date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let y = date.getFullYear();
      let M = month ? '0' + month : month;
      let d = day < 10 ? '0' + day : day;
      let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
      let m =
        date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
      let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return `${y}-${M}-${d} ${h}:${m}:${s}`;
    },
  },
  methods: {
    // 获取记录列表
    getRecordList() {
      this.$allrequest.integral.integralList().then(res => {
        if (!res.code && res.data) {
          let list = res.data || [];
          if (list.length > 0) {
            this.listData = list;
          } else {
            // 如果没有记录，则提示后，返回上一页
            uni.showToast({
              title: this.$t('integral.noRecords'),
              icon: 'none',
              duration: 2000,
            });
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  width: 100%;
  min-height: 100vh;

  .page-content {
    width: 702rpx;
    margin: 0 auto;
    padding-top: 24rpx;

    .list-item {
      width: 100%;
      background: #ffffff;
      border-radius: 14rpx;
      padding: 0 25rpx;

      .record-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 29rpx 0 24rpx 0;
        border-top: 1rpx solid #eeeeee;

        .record-info {
          font-size: 30rpx;
          font-family: PingFang SC;
          font-weight: 400;
          color: #191919;
          padding-right: 10rpx;
          view:last-child {
            font-size: 24rpx;
            font-weight: 500;
            color: #888888;
            margin-top: 15rpx;
          }
        }

        .record-money {
          font-size: 36rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #ff4b33;
        }

        .font-black {
          color: #191919;
        }
      }
    }
    .list-item:first-child .record-item {
      border-top: 0 none;
    }
  }
}
</style>
